package dao.impl;

import dao.BaseDao;
import dao.PowerBatteryDao;
import entity.PowerBattery;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PowerBatteryDaoImpl extends BaseDao implements PowerBatteryDao {
    @Override
    public List<PowerBattery> getAllPowerBattery() {
        String sql = "select * from powerbattery";
        try {
            return executeQuery(sql,rs->{
                List<PowerBattery> list = new ArrayList<>();
                try {
                    while (rs.next()){
                        PowerBattery powerBattery = new PowerBattery();
                        powerBattery.setId(rs.getInt("id"));
                        powerBattery.setCarName(rs.getString("carName"));
                        powerBattery.setCityName(rs.getString("cityName"));
                        powerBattery.setUserName(rs.getString("userName"));
                        powerBattery.setPressureValue(rs.getInt("pressureValue"));
                        powerBattery.setTime(rs.getString("time"));
                        list.add(powerBattery);
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                return list;
            });
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int deletePowerBattery(Integer id) {
        String sql = "delete from powerbattery where id=?";
        try {
            return executeUpdate(sql,id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public int updatePowerBattery(PowerBattery powerBattery) {
        String sql="UPDATE powerbattery SET carName=?,cityName=?,userName=?,pressureValue=?,time=? WHERE id=?";
        try {
            return executeUpdate(sql,powerBattery.getCarName(),powerBattery.getCityName(),powerBattery.getUserName(),powerBattery.getPressureValue(),powerBattery.getTime(),powerBattery.getId());
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public PowerBattery findPowerBatteryById(Integer id) {
        String sql = "select * from powerbattery where id=?";
        try {
            return executeQuery(sql, rs -> {
                PowerBattery powerBattery = null;
                try {
                    while (rs.next()) {
                        powerBattery = new PowerBattery();
                        powerBattery.setId(rs.getInt("id"));
                        powerBattery.setCarName(rs.getString("carName"));
                        powerBattery.setCityName(rs.getString("cityName"));
                        powerBattery.setUserName(rs.getString("userName"));
                        powerBattery.setPressureValue(rs.getInt("pressureValue"));
                        powerBattery.setTime(rs.getString("time"));
                    }
                } catch (SQLException e) {
                    throw new RuntimeException(e);
                }
                return powerBattery;
            },id);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
}
